package org.example.managementsystem.service;

import org.example.managementsystem.model.DeviceRepair;
import org.example.managementsystem.model.DeviceRepairReview;
import org.example.managementsystem.repository.DeviceRepairDao;
import org.example.managementsystem.repository.DeviceRepairReviewDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备报修审核服务层
 * @author 王振祎
 * @date 2025-05-21
 */
@Service
public class DeviceRepairReviewService {

    private final DeviceRepairReviewDao deviceRepairReviewDao;
    private final DeviceRepairDao deviceRepairDao;

    @Autowired
    public DeviceRepairReviewService(DeviceRepairReviewDao deviceRepairReviewDao, DeviceRepairDao deviceRepairDao) {
        this.deviceRepairReviewDao = deviceRepairReviewDao;
        this.deviceRepairDao = deviceRepairDao;
    }

    /**
     * 审核设备报修
     */
    public void reviewRepair(int repairId, boolean isApproved, String description, String reviewerJobNumber) {

        // 先查询表中是否有对应id的审核记录
        DeviceRepairReview existingReview = deviceRepairReviewDao.getReviewByRepairId(repairId);
        if(existingReview != null){
            // 如果有，则更新该记录
            existingReview.setIsApproved(isApproved);
            existingReview.setDescription(description);
            existingReview.setReviewerJobNumber(reviewerJobNumber);
            existingReview.setReviewTime(LocalDateTime.now());
            deviceRepairReviewDao.updateReview(existingReview);

            // 报修审核更新成功后，更新报修表中的审核状态
            deviceRepairDao.updateRepairStatus(repairId, isApproved);
        }
        else{
            DeviceRepairReview review = new DeviceRepairReview();
            review.setRepairId(repairId);
            review.setIsApproved(isApproved);
            review.setDescription(description);
            review.setReviewerJobNumber(reviewerJobNumber);
            review.setReviewTime(LocalDateTime.now());
            deviceRepairReviewDao.insertReview(review);

            // 报修审核更新成功后，更新报修表中的审核状态
            deviceRepairDao.updateRepairStatus(repairId, isApproved);
        }
    }

    public void reviewRepair(DeviceRepairReview review) {

        // 先查询表中是否有对应id的审核记录
        DeviceRepairReview existingReview = deviceRepairReviewDao.getReviewByRepairId(review.getRepairId());

        if(existingReview != null){
            // 如果有，则更新该记录
            existingReview.setIsApproved(review.getIsApproved());
            existingReview.setDescription(review.getDescription());
            existingReview.setReviewerJobNumber(review.getReviewerJobNumber());
            existingReview.setReviewTime(LocalDateTime.now());
            deviceRepairReviewDao.updateReview(existingReview);

            // 报修审核更新成功后，更新报修表中的审核状态
            deviceRepairDao.updateRepairStatus(review.getRepairId(), review.getIsApproved(), true);
        }
        else {
            // 设置审核时间
            review.setReviewTime(LocalDateTime.now());
            deviceRepairReviewDao.insertReview(review);

            // 报修审核更新成功后，更新报修表中的审核状态
            deviceRepairDao.updateRepairStatus(review.getRepairId(), review.getIsApproved(), true);
        }
    }

    /**
     * 更新报修审核记录
     */
    public void updateReview(DeviceRepairReview review) {
        deviceRepairReviewDao.updateReview(review);
    }

    /**
     * 获取所有历史报修审核记录
     */
    public List<DeviceRepairReview> getAllReviews() {
        return deviceRepairReviewDao.getAllReviews();
    }

    /**
     * 根据ID获取报修审核记录
     */
    public DeviceRepairReview getReviewById(int repairId) {
        return deviceRepairReviewDao.getReviewByRepairId(repairId);
    }
}